package com.bawei.module_moto_circle.ui.post.detail

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_moto_circle.model.CommentInfo
import com.bawei.module_moto_circle.model.PostInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 *作者：lxy
 *功能:
 *时间：2025/6/4
 */
@HiltViewModel
class PostDetailViewModel @Inject constructor(private val mRepository: PostDetailRepository) :
    BaseViewModel() {

    var currentPost: PostInfo? = null
    var comments: List<CommentInfo>? = null

    private val mPostState_: MutableStateFlow<RequestEvent> = MutableStateFlow(Loading)
    val mPostState = mPostState_.asStateFlow()

    //当前已请求的comments
    var currentCommentsList: MutableList<CommentInfo> = mutableListOf()
    var formatCommentsList: MutableList<CommentInfo> = mutableListOf()

    //记录第一页当前展开的一级评论
    val needExpendComment = mutableListOf<String>()

    private var currentPage = 1
    private var defaultPageSize = 10
    private var totalPage = 1

    var needRefresh = false


    fun getPostById(postsId: String) {
        val originalSize = formatCommentsList.size
        viewModelScope.launch {
            mRepository.getPostById(postsId)
                .zip(mRepository.queryPostCommentsByPage(postsId, currentPage, defaultPageSize))
                { post, comments ->
                    currentPost = post.data!!
                    //保存评论信息
                    currentCommentsList = comments.data!!.records.toMutableList() ?: mutableListOf()
                    formatCommentsList.clear()
                    //处理获取到的评论数据
                    formatComments(currentCommentsList)
                    needRefresh = originalSize != formatCommentsList.size

                    post
                }.collectLatest {
                    if (it.data != null) {
                        mPostState_.value = Success
                    }
                }
        }

    }

    //获取第一页评论数(包含子级)
    fun get1stPageCommentsNum(): Int {
        var size = 0
        currentCommentsList.filterIndexed { index, _ -> index < defaultPageSize }.forEach {
            size++
            size += it.twoLevel?.size ?: 0
        }
        return size
    }

    private fun formatComments(it: List<CommentInfo>) {
        it.forEach {
            formatCommentsList.add(it)
            it.level = 1
            if (it.twoLevel?.isNotEmpty() == true) {
                if (needExpendComment.any { commentId -> commentId == it.posts_comment_id }) {
                    //需要展开
                    it.twoLevel.forEachIndexed { index, replyComments ->
                        replyComments.level != 1
                        if (index == 0) {
                            //replyComments.topRound = true
                        } else if (index > 0 && index < it.twoLevel.size - 1) {
                            // replyComments.rect = true
                        } else {
                            //  replyComments.bottomRound = true
                        }
                        //replyComments.twoLevelSize = 0
                    }
                    //formatCommentsList.addAll(it.twoLevel)
                } else {
                    //折叠情况
                    val replyComments = it.twoLevel[0]
                    //replyComments.twoLevelSize = it.twoLevel.size
                    //replyComments.isFirstLevel = false
                    // formatCommentsList.add(replyComments)
                }

            }
        }
    }
}